package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Advertise;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.AdvertiseService;
import com.qqtop10.manager.service.ShopService;
import com.qqtop10.manager.utils.LogHelper;

/**
 * @ClassName: AdvertiseController
 * @Description: 广告管理Controller
 * @author ChenXinwei
 * @date 2015/9/18
 */
@RequestMapping("/manager/advertise/")
@Controller
public class AdvertiseController extends BaseController {
	@Autowired
	@Qualifier("advertiseService")
	private AdvertiseService advertiseService;
	@Autowired
	@Qualifier("shopService")
	private ShopService shopService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(Advertise input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			
			Result p=page(advertiseService.count(input), advertiseService.all(input, pagingArgs));
			
			return p;
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("add.do")
	@ResponseBody
	public Result add(HttpServletRequest request, HttpServletResponse response,MultipartFile photo, Integer type)throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		if (type != null && type == 4) {
			// 先将其它的广告图的隐藏
			advertiseService.updateDisableShow(4);
		}
		try {
			Advertise input = new Advertise();
			fill(input);
			input.setPhoto(saveShow(photo, type).getUrl());
			if (input == null || input.getPhoto() == null || input.getText() == null || input.getPath() == null) {
				return err("有些必须的参数没有指定。");
			}
			input.setUuid(newId());
			input.setShop_uuid("");
			input.setType(type);
			input.setCreatetime(now());
			input.setCreator(getLogindUuid());
			if (advertiseService.add(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(), "后台广告管理模块", "添加成功", LogHelper.getLocalIp(getRequest()),
						Tempadmin.getAccount(), "", true);
				return html("操作成功！", input);
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("update.do")
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Advertise input = new Advertise();
		fill(input);
		MultipartFile photo = getFile("photo");
		if (photo != null && !photo.isEmpty()) {
			input.setPhoto(saveShow(photo, null).getUrl());
		}
		if (input == null || input.getId() == null || input.getPhoto() == null || input.getText() == null
				|| input.getPath() == null) {
			return err("有些必须的参数未指定。");
		}
		try {
			input.setUpdatetime(now());
			input.setMender(getLogindUuid());
			boolean ret = advertiseService.update(input);
			if (ret) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(), "后台广告管理模块", "修改成功", LogHelper.getLocalIp(getRequest()),
						Tempadmin.getAccount(), "", true);

				return html("操作成功!", input);
			} else {
				return err("操作失败!");
			}
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("disable.do")
	@ResponseBody
	public Result disable(int[] ids) {
		try {
			Advertise input = new Advertise();
			input.setMender(getLogindUuid());
			input.setUpdatetime(now());
			int count = 0;
			for (int i = 0; i < ids.length; i++) {
				input.setId(ids[i]);
				if (!advertiseService.disable(input)) {
					ids[count] = ids[i];
					++count;
				}
			}
			if (count == 0) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(), "后台广告管理模块", "删除成功", LogHelper.getLocalIp(getRequest()),
						Tempadmin.getAccount(), "", true);

				return ok("操作成功！");
			} else if (count < ids.length) {

				return ok("操作已完成。但有些数据没有删除成功。");
			} else {
				return err("全部操作失败！");
			}
		} catch (Exception e) {
			return excep(e);
		}
	}

	// 禁用就是调用上面的disable方法，此方法为启用
	@RequestMapping("show.do")
	@ResponseBody
	public Result doNotShow(HttpServletRequest request, HttpServletResponse response, Advertise advertise) {
		System.out.println(advertise.getIsdeleted());
		switch (advertise.getType()) {
		case 1:
			// 幻灯片最多只能显示三张
			// 查询当前显示的幻灯片广告数量
			int count = advertiseService.selectTypeShowCount(advertise.getType());
			if (count > 3) {
				return err("当前幻灯片显示张数超过3张限制");
			}
		case 2:
		case 3:
			break;
		case 4:
			// 先将所有的搜索框右边的广告禁止显示
			advertiseService.updateDisableShow(advertise.getType());
		default:
			return err("操作失败");
		}
		// 再将选中的广告开放显示
		int res = advertiseService.updateShow(advertise.getId());
		if (res > 0) {
			return ok("操作成功");
		}
		return err("操作失败");
	}
	
}
